<?php
/**
 * 商品分类管理
 *
 *
 *
 *
 * @好商城提供技术支持 授权请购买shopnc授权
 * @license    http://www.33hao.com
 * @link       交流群号：138182377
 */


defined('In33hao') or exit('Access Invalid!');

class agentControl extends SystemControl
{

    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $model_agent = Model('agent');
        $agent_List =  $model_agent->field('province_id')->distinct(true)->select();
        $area = Model('area');
        foreach($agent_List as $k => $v){
             $area_info = $area->getAreaInfo(array('area_id'=>$v['province_id']));
             $agent_List[$k]['province_name'] = $area_info['area_name'];
        }
        Tpl::output('agent_list', $agent_List);
        Tpl::setDirquna('shop');
        Tpl::showpage('agent.index_sy');
    }

    /**
     * 分类管理
     */
    public function agentOp()
    {
        $model_class = Model('agent');
        $type = $_GET['type'] ? intval($_GET['type']) : 1;
        switch ($_GET['type']) {
            case 1: $title='市代';break;
            case 2: $title='镇代';break;
            default : $title='省代';break;
        }
        $id = $_GET['id'] ? intval($_GET['id']) : '';
        //列表
        if($_GET['province_id']){
            $agent_list = $model_class->where(array('province_id'=>$_GET['province_id'],'type'=>2))->select();
        }else {
            $agent_list = $id ? $model_class->getChildById($type, $id) : $model_class->getCache($type);
        }
        Tpl::output('agent_list', $agent_list);
        Tpl::output('title', $title);
        Tpl::setDirquna('shop');
        Tpl::showpage('agent.index');
    }

    //代理删除
    public function agent_delOp(){
           $id = $_GET['id'];
           $type = $_GET['type'];
           $model_class = Model('agent');
           $area_info = Model()->table('agent')->where(array('id'=>$id))->find();
           if($type==2){
               $where['city_id'] = $area_info['city_id'];
               $where['type'] = $type;
               $where['id'] = ['neq',$id];
               // 查看该代理是否存在下级代理
               $child = $model_class->getAgentList($where);
               if($child){
                   showMessage('该代理已有下级代理,请删除下级代理', '', 'html', 'error');
               }
           }
           $res = $model_class->del($id);

           if($res){
               $model_class->dropCache();
               showMessage("删除成功",'index.php?act=agent&op=index');

           }else{
               showMessage("删除失败请重试",'index.php?act=agent&op=index');
           }
    }

    /**
     * 代理商添加
     */
    public function addOp()
    {
        $lang = Language::getLangContent();
        if (chksubmit()) {
           // 填写推荐人
            if($_POST['invite_agent']){
                $invite_agent = $_POST['invite_agent'];
                $invite_agent_member_info = Model('member')->getMemberInfo(array('member_name'=>$invite_agent));
                if(!$invite_agent_member_info){
                    showMessage('该推荐人账号不存在', '', 'html', 'error');
                }
                $data['invite_id'] = $invite_agent_member_info['member_id'];
            }
            $where['title'] = $_POST['title'];;
            $where['type'] =  $_POST['type'];
            $type = $_POST['type'];
            if($type==2){
                  $where['city_id'] = $_POST['city_id'];
                  $mtype = 4;
            }
            if($type==3){
                  $where['area_id'] = $_POST['area_id'];
                  $mtype = 5;
            }

            $res = Model('agent')->getAgentInfo($where);
            if($res){
                showMessage('该地区已经存在该代理商', '', 'html', 'error');
            }
            $memberName = $_POST['member_name'];
            $memberPoints = $_POST['member_points'];
            $memberPasswd = (string)$_POST['member_passwd'];
            if (strlen($memberName) < 3 || strlen($memberName) > 15)
            {
                showMessage('账号名称必须是3~15位', '', 'html', 'error');
            }
            if (strlen($memberPasswd) < 6) {
                showMessage('登录密码不能短于6位', '', 'html', 'error');
            }
            if (!$this->checkMemberName($memberName))
            {
                showMessage('用户账号已被占用', '', 'html', 'error');
            }
            if (preg_match('/^1[34578]\d{9}$/i', $memberName)) {
                if (!$this->checkMobile($memberName))
                {
                    showMessage('手机号已被注册', '', 'html', 'error');
                }
                $member['member_mobile_bind'] = 1;
            }
            if (!is_numeric($memberPoints) && $memberPoints < 0)
            {
                showMessage('积分必须是大于等于0的纯数字', '', 'html', 'error');
            }
            try {
                if ($member['member_mobile_bind']) {
                    $member['member_mobile'] = $memberName;
                } else {
                    $member['member_name'] = $memberName;
                }
                $member['member_passwd'] = $memberPasswd;
                $member['member_points'] = $memberPoints;
                if($type==4){
                    $member['invite_ty'] = $invite_agent_member_info['member_id']?$invite_agent_member_info['member_id']:0;
                    $mtype = 6 ;
                }else{
                    $member['invite_agent'] = $invite_agent_member_info['member_id']?$invite_agent_member_info['member_id']:0;
                }
                $member['member_provinceid'] = $_POST['province_id'];
                $member['member_cityid'] = $_POST['city_id'];
                $member['member_areaid'] = $_POST['area_id'];
                $member['member_areainfo'] = $_POST['region'];
                $member['type'] = $mtype;
                $data['member_id'] = Model('member')->addMember($member);
            } catch (Exception $ex) {
                showMessage('用户账号新增失败', '', 'html', 'error');
            }
            $data['title'] = $_POST['title'];
            $data['province_id'] = $_POST['province_id'];
            $data['city_id'] = $_POST['city_id'];
            $data['area_id'] = $_POST['area_id'];
            $data['area_info'] = $_POST['region'];
            $data['type'] = $type;
            $res = Model('agent')->addAgent($data);

            if (!$res) {
                showMessage('用户账号新增失败', '', 'html', 'error');
            }
            Model('agent')->dropCache();
            showMessage('添加商圈代理成功', 'index.php?act=agent&op=index');
        } else {
            Tpl::output('type', $_GET['type']);
            Tpl::output('title', $_GET['title']);
            Tpl::setDirquna('shop');
            Tpl::showpage('agent.add');
        }

    }

    /**
     * 编辑
     */
    public function editOp()
    {
        if (chksubmit()) {
            $data['title'] = $_POST['title'];
            if ($_POST['province_id']) {
                $data['province_id'] = $_POST['province_id'];
            }
            if ($_POST['city_id']) {
                $data['city_id'] = $_POST['city_id'];
            }
            if ($_POST['area_id']) {
                $data['area_id'] = $_POST['area_id'];
            }
            if ($_POST['area_info']) {
                $data['area_info'] = $_POST['area_info'];
            }
            $data['type'] = $_POST['type'];
            $where['id'] =  $_POST['id'];
            $res = Model('agent')->editAgent($where,$data);
            if (!$res) {
                showMessage('编辑代理失败', '', 'html', 'error');
            }
            Model('agent')->dropCache();
            showMessage('编辑代理成功', 'index.php?act=agent&op=index');
        } else {
            $agent = Model('agent')->getAgentById($_GET['type'],$_GET['id']);
            Tpl::output('agent', $agent);
            Tpl::setDirquna('shop');
            Tpl::showpage('agent.edit');
        }
    }

    private function checkMemberName($memberName)
    {
        // 判断store_joinin是否存在记录
        $count = (int)Model('store_joinin')->getStoreJoininCount(array(
            'member_name' => $memberName,
        ));
        if ($count > 0)
            return false;

        return !Model('member')->getMemberCount(array(
            'member_name' => $memberName,
        ));
    }

    private function checkMobile($memberName)
    {
        $member = Model('member')->getMemberInfo(array('member_mobile'=> $memberName));//检查手机号是否已被注册
        if(!empty($member)) {
            showDialog('手机号已被注册','','error');
        }
    }

    public function check_titleOp()
    {
        $where['title'] = $_GET['title'];
        $where['type'] = $_GET['type'];
        $id = $_GET['id'];
        if ($id) {
            $where['id'] = ['NEQ',$_GET['id']];
        }
        $agent = Model('agent')->getAgentInfo($where);//检查代理名称是否已被注册
        if($agent) {
            echo 'false';exit;
        }
        echo 'true';exit;
    }

    public function ajaxOp()
    {
        $id = $_GET['id'];
        $agent = Model('agent')->getAgentInfo(['id'=>$id]);
        $type = $agent['type'];
        $where['id'] = ['NEQ',$id];
        $where['type'] = $type;
        $where[$_GET['branch']] = $_GET['value'];
        if(Model('agent')->getAgentInfo($where)) {//检查代理名称是否已被注册
            exit(json_encode(array('result'=>false,'message'=>'该名称已存在')));
        }
        $data[$_GET['branch']] = $_GET['value'];
        $condition['id'] = $id;
        $agent = Model('agent')->editAgent($condition);
        if($agent) {
            exit(json_encode(array('result'=>true,'message'=>'修改成功')));
        }
        exit(json_encode(array('result'=>false,'message'=>'修改失败')));
    }
}
